package com.itheima.admin.controller;

import com.itheima.admin.service.Blogservice;
import com.itheima.xo.common.Result;
import com.itheima.xo.pojo.Blog;
import com.itheima.xo.vo.BlogVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author :WuJunJie
 * packageName :com.itheima.admin.controller
 * fileName :BlogController
 * creationTime :2022-10-30 21:26
 */
@RestController
@RequestMapping("blog")
@Slf4j
public class BlogController {
    @Autowired
    private Blogservice blogservice;

    @PostMapping("add")
    public Result add(@RequestBody Blog blog) {
        Boolean legal = isLegal(blog);
        if (legal) {
            Boolean add = blogservice.add(blog);
            if (add) {
                return Result.success(null);
            }
        }
        return Result.error();
    }

    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable String id) {
        if (id == null || id.trim().length() == 0) {
            log.info("需要删除的ID不能为空");
            return Result.error();

        }
        Boolean delete = blogservice.delete(id);
        if (delete) {
            return Result.success(null);
        }
        return Result.error();
    }

    @PutMapping("update")
    public Result update(@RequestBody Blog blog) {
        Boolean legal = isLegal(blog);
        if (legal) {
            Boolean update = blogservice.update(blog);
            if (update) {
                return Result.success(null);
            }
        }
        return Result.error();
    }

    @GetMapping("findAll")
    public Result findAll() {
        List<BlogVo> blogVos = blogservice.findAll();
        return Result.success(blogVos);
    }

    private Boolean isLegal(Blog blog) {
        String title = blog.getTitle();
        if (title == null || title.trim().length() == 0) {
            log.info("标题不能为空");
            return false;
        }
        if (title.length() > 50) {
            log.info("标题“{}”过长", title);
            return false;
        }


        String author = blog.getAuthor();
        if (author == null || author.trim().length() == 0) {
            log.info("作者名字不能为空");
            return false;
        }
        if (author.length() > 10) {
            log.info("作者名字“{}”过长");
            return false;
        }


        if (blog.getCategoryId() == null || blog.getCategoryId().trim().length() == 0) {
            log.info("分类ID不能为空");
            return false;
        }

        if (blog.getContent() == null || blog.getContent().trim().length() == 0) {
            log.info("博客内容不能为空");
            return false;
        }
        return true;
    }
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable String id){
        return Result.success(blogservice.findPoById(id));
    }
//
//    public Result findByTitle (String title){ return null;}
//    public Result findByCategoryId(String categoryId){ return null;}
}
